package com.gbx.ximalayating.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.gbx.ximalayating.R;
import com.gbx.ximalayating.entity.RecommendTwo;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Administrator on 2016/12/8.
 */

public class HotRecommendsAdapter extends RecyclerView.Adapter<HotRecommendsAdapter.ViewHolder> implements View.OnClickListener {

    private Context context;
    private List<RecommendTwo.HotRecommends.ConEntity> list;
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public HotRecommendsAdapter(Context context, List<RecommendTwo.HotRecommends.ConEntity> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(v);
        }
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    //
//    @Override
//    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//            ViewHolder holder = new ViewHolder(LayoutInflater.from(context).inflate(R.layout.recommend_item,parent,false));
//            return holder;
//    }
//
//    @Override
//    public void onBindViewHolder(ViewHolder holder, int position) {
//        RecommendTwo.HotRecommends.ConEntity conEntity = list.get(position);
//        holder.titletext.setText(conEntity.getTitle());
//        Picasso.with(context).load(conEntity.getList().get(0).getCoverLarge()).into(holder.hotimage1);
//        Picasso.with(context).load(conEntity.getList().get(1).getCoverLarge()).into(holder.hotimage2);
//        Picasso.with(context).load(conEntity.getList().get(2).getCoverLarge()).into(holder.hotimage3);
//        holder.hotsum1.setText(conEntity.getList().get(0).getTrackTitle());
//        holder.hotsum2.setText(conEntity.getList().get(1).getTrackTitle());
//        holder.hotsum3.setText(conEntity.getList().get(2).getTrackTitle());
//        holder.hotres1.setText(conEntity.getList().get(0).getTitle());
//        holder.hotres2.setText(conEntity.getList().get(1).getTitle());
//        holder.hotres3.setText(conEntity.getList().get(2).getTitle());
//    }
//
//    @Override
//    public int getItemCount() {
//
//        return list.size();
//    }
//
    public static class ViewHolder extends RecyclerView.ViewHolder{

        private TextView titletext,hotsum1,hotsum2,hotsum3,hotres1,hotres2,hotres3;
        private ImageView hotimage1,hotimage2,hotimage3;
        public ViewHolder(View itemView) {
            super(itemView);
            titletext = ((TextView) itemView.findViewById(R.id.titletext));
            hotsum1 = ((TextView) itemView.findViewById(R.id.hotsum_1));
            hotsum2 = ((TextView) itemView.findViewById(R.id.hotsum_2));
            hotsum3 = ((TextView) itemView.findViewById(R.id.hotsum_3));
            hotres1 = ((TextView) itemView.findViewById(R.id.hotres_1));
            hotres2 = ((TextView) itemView.findViewById(R.id.hotres_2));
            hotres3 = ((TextView) itemView.findViewById(R.id.hotres_3));
            hotimage1 = ((ImageView) itemView.findViewById(R.id.hotimage_1));
            hotimage2 = ((ImageView) itemView.findViewById(R.id.hotimage_2));
            hotimage3 = ((ImageView) itemView.findViewById(R.id.hotimage_3));
        }
    }

    /**
     * -----------------------------------------------------------------
     * */
    private RecyclerView mRecyclerView;
    private View VIEW_FOOTER;
    private View VIEW_HEADER;
    private View.OnClickListener listener;
    //Type
    private int TYPE_NORMAL = 1000;
    private int TYPE_HEADER = 1001;
    private int TYPE_FOOTER = 1002;

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_FOOTER) {
            return new ViewHolder(VIEW_FOOTER);
        } else if (viewType == TYPE_HEADER) {
            return new ViewHolder(VIEW_HEADER);
        } else {
            View itemView = LayoutInflater.from(context).inflate(R.layout.recommend_item, parent, false);
            ViewHolder holder = new ViewHolder(itemView);
//            itemView.setOnClickListener(this);
            return holder;
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (!isHeaderView(position) && !isFooterView(position)) {
            if (haveHeaderView()) position--;
            RecommendTwo.HotRecommends.ConEntity conEntity = list.get(position);
            holder.titletext.setText(conEntity.getTitle());
            Picasso.with(context).load(conEntity.getList().get(0).getCoverLarge()).into(holder.hotimage1);
            Picasso.with(context).load(conEntity.getList().get(1).getCoverLarge()).into(holder.hotimage2);
            Picasso.with(context).load(conEntity.getList().get(2).getCoverLarge()).into(holder.hotimage3);
            holder.hotimage1.setOnClickListener(this);
            holder.hotimage2.setOnClickListener(this);
            holder.hotimage3.setOnClickListener(this);
            holder.hotsum1.setText(conEntity.getList().get(0).getTrackTitle());
            holder.hotsum2.setText(conEntity.getList().get(1).getTrackTitle());
            holder.hotsum3.setText(conEntity.getList().get(2).getTrackTitle());
            holder.hotres1.setText(conEntity.getList().get(0).getTitle());
            holder.hotres2.setText(conEntity.getList().get(1).getTitle());
            holder.hotres3.setText(conEntity.getList().get(2).getTitle());
            holder.itemView.setTag(list.get(position));
        }
    }

    @Override
    public int getItemCount() {
        int count = (list == null ? 0 : list.size());
        if (VIEW_FOOTER != null) {
            count++;
        }

        if (VIEW_HEADER != null) {
            count++;
        }
        return count;
    }

    @Override
    public int getItemViewType(int position) {
        if (isHeaderView(position)) {
            return TYPE_HEADER;
        } else if (isFooterView(position)) {
            return TYPE_FOOTER;
        } else {
            return TYPE_NORMAL;
        }
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        try {
            if (mRecyclerView == null && mRecyclerView != recyclerView) {
                mRecyclerView = recyclerView;
            }
            ifGridLayoutManager();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private View getLayout(int layoutId) {
        return LayoutInflater.from(context).inflate(layoutId, null);
    }

    public void addHeaderView(View headerView) {
        if (haveHeaderView()) {
            throw new IllegalStateException("hearview has already exists!");
        } else {
            //避免出现宽度自适应
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            headerView.setLayoutParams(params);
            VIEW_HEADER = headerView;
            ifGridLayoutManager();
            notifyItemInserted(0);
        }

    }

    public void addFooterView(View footerView) {
        if (haveFooterView()) {
            throw new IllegalStateException("footerView has already exists!");
        } else {
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            footerView.setLayoutParams(params);
            VIEW_FOOTER = footerView;
            ifGridLayoutManager();
            notifyItemInserted(getItemCount() - 1);
        }
    }

    private void ifGridLayoutManager() {
        if (mRecyclerView == null) return;
        final RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            final GridLayoutManager.SpanSizeLookup originalSpanSizeLookup =
                    ((GridLayoutManager) layoutManager).getSpanSizeLookup();
            ((GridLayoutManager) layoutManager).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    return (isHeaderView(position) || isFooterView(position)) ?
                            ((GridLayoutManager) layoutManager).getSpanCount() :
                            1;
                }
            });
        }
    }

    private boolean haveHeaderView() {
        return VIEW_HEADER != null;
    }

    public boolean haveFooterView() {
        return VIEW_FOOTER != null;
    }

    private boolean isHeaderView(int position) {
        return haveHeaderView() && position == 0;
    }

    private boolean isFooterView(int position) {
        return haveFooterView() && position == getItemCount() - 1;
    }

    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view);
    }

}
